package cn.itcast.utils;

import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.sql.rowset.CachedRowSet;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;


public class XmlUtil {

	public static void dump(ResultSet rs) {
		try {
			ResultSetMetaData rsmd = rs.getMetaData();

			Document document = DocumentHelper.createDocument();
			Element root = document.addElement("SQL");

			Element sqlValueElement = root.addElement("SQLVALUE").addAttribute("FieldCount",
					String.valueOf(rsmd.getColumnCount()));

			Element typeNameElement = sqlValueElement.addElement("TypeName");

			for (int i = 0; i < rsmd.getColumnCount(); i++) {
				int type = rsmd.getColumnType(i + 1);
				typeNameElement.addAttribute("Type" + i, String.valueOf(type));
			}

			Element fieldNameElement = typeNameElement.addElement("FieldName");
			
			for (int i = 0; i < rsmd.getColumnCount(); i++) {
				String columnName = rsmd.getColumnLabel(i + 1);
				fieldNameElement.addAttribute("Field" + i, columnName);
			}

			int rowNum = 0;

			while (rs.next()) {
				rowNum++;

				Element rowElement = fieldNameElement.addElement("R" + rowNum);

				int total_rows = rsmd.getColumnCount();
				for (int i = 0; i < total_rows; i++) {
					String columnName = rsmd.getColumnLabel(i + 1);
					String val = "";
					switch (rsmd.getColumnType(i + 1)) {
					case java.sql.Types.ARRAY:
						val = rs.getArray(columnName).toString();
						break;
					case java.sql.Types.BIGINT:
						val = String.valueOf(rs.getInt(columnName));
						break;
					case java.sql.Types.BOOLEAN:
						val = String.valueOf(rs.getBoolean(columnName));
						break;
					case java.sql.Types.BLOB:
						val = rs.getBlob(columnName).toString();
						break;
					case java.sql.Types.DOUBLE:
						val = String.valueOf(rs.getDouble(columnName));
						break;
					case java.sql.Types.FLOAT:
						val = String.valueOf(rs.getFloat(columnName));
						break;
					case java.sql.Types.INTEGER:
						val = String.valueOf(rs.getInt(columnName));
						break;
					case java.sql.Types.NVARCHAR:
						val = rs.getNString(columnName);
						break;
					case java.sql.Types.VARCHAR:
						val = rs.getString(columnName);
						break;
					case java.sql.Types.TINYINT:
						val = String.valueOf(rs.getInt(columnName));
						break;
					case java.sql.Types.SMALLINT:
						val = String.valueOf(rs.getInt(columnName));
						break;
					case java.sql.Types.DATE:
						val = rs.getDate(columnName).toString();
						break;
					case java.sql.Types.TIMESTAMP:
						val = rs.getTimestamp(columnName).toString();
						break;
					default:
						val = rs.getObject(columnName).toString();
						break;
					}
					
					rowElement.addAttribute("V" + i, val);
				}
			}

			sqlValueElement.addAttribute("RecordCount", String.valueOf(rowNum));

	    	OutputFormat format = OutputFormat.createPrettyPrint();
	    	format.setEncoding("utf-8");
	    	
	    	FileWriter fileWriter = new FileWriter("C:\\Users\\Administrator\\Desktop\\output.xml");
		
	        XMLWriter writer = new XMLWriter(fileWriter, format);
	        writer.write( document );
	        writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
    public static Document createDocument() {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("SQL");
        
        Element sqlValueElement = root.addElement("SQLVALUE")
        	.addAttribute("FieldCount", "4")
        	.addAttribute("RecordCount", "5");
        
        Element typeNameElement = sqlValueElement.addElement("TypeName")
        	.addAttribute("Type0", "1")
        	.addAttribute("Type1", "2");
        
        Element FieldNameElement = typeNameElement.addElement("FieldName")
        	.addAttribute("field0", "a")
        	.addAttribute("field1", "b");
        
        FieldNameElement.addElement("R0")
        	.addAttribute("V0", "123")
        	.addAttribute("V1", "456");
        
        FieldNameElement.addElement("R1")
        	.addAttribute("V0", "789")
        	.addAttribute("V1", "abc");

        return document;
    }
	
    public static void write(Document document) throws IOException {

    	OutputFormat format = OutputFormat.createPrettyPrint();
    	format.setEncoding("utf-8");
    	
    	FileWriter fileWriter = new FileWriter("C:\\Users\\Administrator\\Desktop\\output.xml");
    	
        // lets write to a file
        XMLWriter writer = new XMLWriter(fileWriter, format);
        writer.write( document );
        writer.close();

        // Pretty print the document to System.out
        
        writer = new XMLWriter(System.out, format);
        writer.write( document);

        // Compact format to System.out
        format = OutputFormat.createCompactFormat();
        writer = new XMLWriter(System.out, format);
        writer.write(document);
    }
    
    public static void main(String[] args) throws IOException, SQLException {
/*		Document document = createDocument();
		write(document);*/

    	int jdkVersion = Integer.parseInt(System.getProperty("java.version").split("\\.")[1]);
    	
    	System.out.println(jdkVersion);
    	// jdk7+
/*		RowSetFactory factory = RowSetProvider.newFactory();
		CachedRowSet rs = factory.createCachedRowSet();*/
    	
    	// jdk6
    	CachedRowSet rs = new com.sun.rowset.CachedRowSetImpl();
	}
}
